﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NHibernate;
using Web.Model;
using System.Web.Mvc;
using Web.Extensions;
using System.Configuration;

namespace Web.HelperClasses
{
    public class AdminNotifier
    {
        private ISession session;
        private List<string> adminEmails;

        private const string NewIdeaTemplate = @"Пользователь {0} добавил проект <a href='{2}'>{1}</a>.<br/>
Суть проекта:<br/>
{3}<br/>
Дата:{4}
";
        private const string NewNewsTemplate = @"Пользователь {0} добавил новость <a href='{2}'>{1}</a>.<br/>
Краткое содержание:<br/>
{3}<br/>
Полный текст:<br/>
{4}<br/>
Добавлено:{5}
";

        public AdminNotifier(ISession session)
        {
            this.session = session;
            string emails = ConfigurationManager.AppSettings["adminEmails"];
            adminEmails = emails.Split(new string []{","}, StringSplitOptions.RemoveEmptyEntries).ToList();
            adminEmails.ForEach(s => s = s.Trim());
        }

        public void NotifyIdeaCreated(Idea idea)
        {
            UrlHelper urlHelper = UrlHelperExtensions.GetUrlHelper();
            string userString = "";
            if(idea.Author == null)
            {
                userString = "Анонимус";
            }else{
                userString = String.Format("<a href='{1}'>{0}</a>", 
                    HttpUtility.HtmlEncode(idea.Author.DisplayName),
                    urlHelper.ActionAbsolute("View", "User", new { Id = idea.Author.Id }).AbsoluteUri);
            }
            string body = String.Format(NewIdeaTemplate,
                userString,
                HttpUtility.HtmlEncode(idea.Name),
                urlHelper.ActionAbsolute("View", "Idea", new { Id = idea.Id }).AbsoluteUri,
                HttpUtility.HtmlEncode(idea.Description),
                HttpUtility.HtmlEncode(idea.BeginningDate.ToString("dd MMMM yyyy (ddd), HH:mm"))
                );
            string subject = "Добавлен новый проект '" + idea.Name + "'";
            foreach (string email in adminEmails)
            {
                Notification newNotification = new Notification()
                {
                    Subject = subject,
                    // Recipient = organizer,
                    Email = email,
                    Body = body
                };
                session.Save(newNotification);
            }
        }

        public void NotifyProjectCreated(Project project)
        {
            UrlHelper urlHelper = UrlHelperExtensions.GetUrlHelper();
            string userString = "";
            if (project.Organizer == null)
            {
                userString = "Анонимус";
            }
            else
            {
                userString = String.Format("<a href='{1}'>{0}</a>",
                    HttpUtility.HtmlEncode(project.Organizer.DisplayName),
                    urlHelper.ActionAbsolute("View", "User", new { Id = project.Organizer.Id }).AbsoluteUri);
            }
            string body = String.Format(NewIdeaTemplate,
                userString,
                HttpUtility.HtmlEncode(project.Name),
                urlHelper.ActionAbsolute("View", "Project", new { Id = project.Id }).AbsoluteUri,
                HttpUtility.HtmlEncode(project.Description),
                HttpUtility.HtmlEncode(project.Beginning.Value.ToString("dd MMMM yyyy (ddd), HH:mm"))
                );
            string subject = "Добавлен новый проект '" + project.Name + "'";
            foreach (string email in adminEmails)
            {
                Notification newNotification = new Notification()
                {
                    Subject = subject,
                    // Recipient = organizer,
                    Email = email,
                    Body = body
                };
                session.Save(newNotification);
            }
        }

        public void NotifyNewsCreated(News news)
        {
            UrlHelper urlHelper = UrlHelperExtensions.GetUrlHelper();
            string userString = "";
            if (news.Author == null)
            {
                userString = "Анонимус";
            }
            else
            {
                userString = String.Format("<a href='{1}'>{0}</a>",
                    HttpUtility.HtmlEncode(news.Author.DisplayName),
                    urlHelper.ActionAbsolute("View", "User", new { Id = news.Author.Id }).AbsoluteUri);
            }
            string body = String.Format(NewNewsTemplate,
                userString,
                HttpUtility.HtmlEncode(news.Title),
                urlHelper.ActionAbsolute("View", "News", new { Id = news.Id }).AbsoluteUri,
                HttpUtility.HtmlEncode(news.ShortContent),
                HttpUtility.HtmlEncode(news.Content),
                HttpUtility.HtmlEncode(news.DateCreated.ToString("dd MMMM yyyy (ddd), HH:mm"))
                );
            string subject = "Добавлена новость '" + news.Title + "'";
            foreach (string email in adminEmails)
            {
                Notification newNotification = new Notification()
                {
                    Subject = subject,
                    // Recipient = organizer,
                    Email = email,
                    Body = body
                };
                session.Save(newNotification);
            }
        }
    }
}